package com.ayu.scalalearn.chapter06

/**
  * @Author 徐林玉
  * @Description Test09Singleton
  * @Date 2022/7/29 13:55
  * @Version 1.0
  */
object Test09Singleton {

  def main(args: Array[String]): Unit = {


    val p1 = PersonTest09.getInstance();

    println(p1)

    val p2 = PersonTest09.getInstance();

    println(p2)

  }

}


class PersonTest09 private(name: String, age: Int) {


  def hello(): Unit = {

    println("hello " + name)
  }
}


//饿汉模式
//object PersonTest09 {
//
//  private val person: PersonTest09 = new PersonTest09("ayu", 18)
//
//  def getInstance(): PersonTest09 = person
//
//
//}


//懒汉模式
object PersonTest09 {
  private var person: PersonTest09 = _

  def getInstance(): PersonTest09 = {

    if (person == null) {

      person = new PersonTest09("ayu", 19)
    }

    person
  }
}
